/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-04-18 23:54:04
 * Copyright (c) 2015- -版权所有
 */
package com.joysuch.wwyt.core.service.impl;

import com.joysuch.wwyt.api.base.dto.ApiRoleDto;
import com.joysuch.wwyt.core.bean.BaseRoleDetailBean;
import com.joysuch.wwyt.core.bean.BaseRoleEditBean;
import com.joysuch.wwyt.core.bean.BaseRoleListBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.entity.BaseRole;
import com.joysuch.wwyt.core.entity.dsl.QBaseRole;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.mapper.BaseRoleMapper;
import com.joysuch.wwyt.core.repository.BaseRoleDao;
import com.joysuch.wwyt.core.service.BaseRoleService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

import java.util.List;

import static com.joysuch.wwyt.core.bean.BaseRoleListBean.SearchBaseRole;


@Service
@Transactional
public class BaseRoleServiceImpl implements BaseRoleService {

    @Autowired
    private BaseRoleDao dao;
    
    @Autowired
    private BaseRoleMapper baseRoleMapper;

    @Override
    public BaseRole add(BaseRoleEditBean entity) {
        int count = dao.countByName(entity.getName());
        if (count > 0) {
            throw new IllegalArgumentException("角色名称已存在");
        }
        BaseRole content = new BaseRole();
        BeanUtils.copyProperties(entity, content, "id");
        dao.save(content);
        return content;
    }

    @Override
    public void update(BaseRoleEditBean entity) {
        BaseRole content = dao.findById(entity.getId()).get();
        BeanUtils.copyProperties(entity, content, "id");
        dao.save(content);
        int count = dao.countByName(entity.getName());
        if (count > 1) {
            throw new IllegalArgumentException("角色名称已存在");
        }
    }

    @Override
    public Page<BaseRoleListBean> findByPage(Pageable page, SearchBaseRole condition) {
        Predicate predicate = prepareListSearchCondition(condition);

        Page<BaseRole> data = predicate == null ? dao.findAll(page) : dao.findAll(predicate, page);

        return PageDataUtils.convertPageData(data, new PageDataConvert<BaseRoleListBean, BaseRole>() {

            @Override
            public BaseRoleListBean convert(BaseRole f) {
                BaseRoleListBean bean = new BaseRoleListBean();
                BeanUtils.copyProperties(f, bean);
                return bean;
            }

        });
    }

    private Predicate prepareListSearchCondition(SearchBaseRole condition) {
        Predicate p = null;
        QBaseRole q = QBaseRole.baseRole;
        if (condition.getName() != null) {
            p = ExpressionUtils.and(p, q.name.contains(condition.getName()));
        }
        if (condition.getType() != null) {
            p = ExpressionUtils.and(p, q.type.eq(condition.getType()));
        }
        if (condition.getShowOrder() != null) {
            p = ExpressionUtils.and(p, q.showOrder.eq(condition.getShowOrder()));
        }
        if (condition.getIcon() != null) {
            p = ExpressionUtils.and(p, q.icon.eq(condition.getIcon()));
        }
        return p;
    }

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
//校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     *
     * @param id 要删除的业务ID
     * @throws BusinessRestrictionsException 抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {
    }

    @Override
    public BaseRole findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public BaseRoleDetailBean getById(Long id) {
        BaseRole entity = findById(id);
        if (entity == null) {
            return null;
        }
        BaseRoleDetailBean bean = new BaseRoleDetailBean();
        BeanUtils.copyProperties(entity, bean);
        return bean;
    }

    @Override
    public List<ApiRoleDto> getListForApi(String roleName, String remark, Long tenentId, String orgCode) {
        return baseRoleMapper.getListForApi(roleName,remark,tenentId,orgCode);
    }
    
    @Override
    public com.baomidou.mybatisplus.extension.plugins.pagination.Page<ApiRoleDto> getListByPageForApi(Integer page, Integer size, String roleName, String remark, Long tenentId, String orgCode) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ApiRoleDto> apiRoleVoPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page+1,size);
        return baseRoleMapper.getListByPageForApi(apiRoleVoPage,roleName,remark,tenentId,orgCode);
    }
    
   
}
